package com.iuie.ns.system.beans;

import com.iuie.ns.frame.utils.DateUtils;

class ReportWeekParams extends ReportParams {

	public ReportWeekParams(String statisticSize, String statisticRange, String startTime, String endTime) {
		this.endTime = endTime;
		this.startTime = startTime;
		this.statisticSize = statisticSize;
		this.statisticRange = statisticRange;
		this.range = Integer.parseInt(statisticRange);
	}

	@Override
	protected ReportParams init() {
		String currentDate = DateUtils.getCurrentDate();
		int week = DateUtils.getDayOfWeek(currentDate) - 1; // 当天是星期几
		int sub = (this.range - 1) * 7 + week; // 当天减去 sub 天就是startTime
		this.startTime = DateUtils.addDate(currentDate, -sub);
		this.endTime = currentDate;
		
		// 计算categories 和queryParams
		this.compute();
		return this;
	}

	private void compute() {
		// 从当天开始，往前算x周
		for (int i = this.range - 1; i >= 0; i --) {
			// 构造前端显示的category
			String currDate = DateUtils.getCurrentDate();
			int step = DateUtils.getDayOfWeek(currDate) - 1;
			int sub1 = i * 7 + step;
			String d1 = DateUtils.addDate(currDate, -sub1);
			
			int sub2 = (i - 1) * 7 + step + 1;
			String d2 = DateUtils.addDate(currDate, -sub2);
			if (DateUtils.compareDate(d2, currDate) == 3) {
				d2 = currDate;
			}
			
			String category = DateUtils.formatDate(d1, "yyyy-MM-dd", "MM月dd日") + "-" + DateUtils.formatDate(d2, "yyyy-MM-dd", "MM月dd日");
			categories.add(category);
			
			// 构造后台查询的category
			String qc = DateUtils.getYearWeek(d1);
			this.queryParams.add(qc);
		}
	}
}
